package com.washmadam.service.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.washmadam.service.common.constants.Constant;

import common.service.base.interceptor.ApiRequestLimitInterceptor;
import common.service.base.interceptor.MobileInterceptor;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter  {

	@Bean
    public MobileInterceptor getMobileInterceptor() {
        return new MobileInterceptor();
    }

    @Bean
    public ApiRequestLimitInterceptor getApiRequestLimitInterceptor() {
        return new ApiRequestLimitInterceptor();
    }
	
	@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
	
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getMobileInterceptor())
                .addPathPatterns(Constant.URL_PREFIX + "/**"); 
        registry.addInterceptor(getApiRequestLimitInterceptor())
                .addPathPatterns(Constant.URL_PREFIX + "/**");
    }
}